package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.House;
import com.ruoyi.system.domain.ViewRecord;
import com.ruoyi.system.service.IHouseService;
import com.ruoyi.system.service.IViewRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 申请看房记录Controller
 *
 * @author ...
 * @date 2022-03-18
 */
@RestController
@RequestMapping("/system/record")
public class ViewRecordController extends BaseController {
    @Autowired
    private IViewRecordService viewRecordService;
    @Autowired
    private IHouseService houseService;
    /**
     * 查询申请看房记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ViewRecord viewRecord) {
        startPage();
        List<ViewRecord> list = viewRecordService.selectViewRecordList(viewRecord);
        return getDataTable(list);
    }

    /**
     * 获取申请看房记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(viewRecordService.selectViewRecordById(id));
    }

    /**
     * 新增申请看房记录
     */
    @PostMapping
    public AjaxResult add(@RequestBody ViewRecord viewRecord) {
        House house = houseService.selectHouseById(viewRecord.getHousuId());
        viewRecord.setHouseName(house.getName());
        viewRecord.setStatus("待回复");
        viewRecord.setCreateUser(SecurityUtils.getUserId().intValue());
        viewRecord.setCreateTime(DateUtils.getNowDate());
        return toAjax(viewRecordService.insertViewRecord(viewRecord));
    }

    /**
     * 查询我的记录
     * @return
     */
    @GetMapping("/myRecord")
    public AjaxResult getMyRecord(){
        List<ViewRecord> myRecord = viewRecordService.getMyRecord();
        return AjaxResult.success(myRecord);
    }
    /**
     * 修改申请看房记录
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ViewRecord viewRecord) {
        viewRecord.setUpdateUser(SecurityUtils.getUserId().intValue());
        viewRecord.setUpdateTime(DateUtils.getNowDate());
        return toAjax(viewRecordService.updateViewRecord(viewRecord));
    }

    /**
     * 删除申请看房记录
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(viewRecordService.deleteViewRecordByIds(ids));
    }
}
